package com.lancoo.znbkxx.teacher.ui.fragment

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.model.data.ClassEffective
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.lancoo.znbkxx.teacher.widget.ClassMenu
import kotlinx.android.synthetic.main.view_publish_result.*
import kotlinx.android.synthetic.main.view_publish_result.view.*

/**
 *@author Admin
 *@date 2021/2/23
 *
 */
class PublishResultDialog : AppCompatDialogFragment(), BaseQuickAdapter.OnItemClickListener {
    private val info by lazy { arguments?.getParcelableArrayList<ClassEffective>("release") }


    private val classPopupWindow by lazy {
        val list = mutableListOf<String>()
        info!!.forEach {
            list.add(it.CourseClassName)
        }
        ClassMenu(requireContext(), list, this)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return inflater.inflate(R.layout.view_publish_result, container, false)
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        view.apply {
            tv_num.text = "${info!![0]?.RecordEffective}%"
            tv_num_add.text = "(+${info!![0].ContributionRate}%)"
            tv_percent.text = "${info!![0].KlgRate}%"
            tv_percent_add.text = "(+${info!![0].KlgContributionRate}%)"
//            val spanString by lazy {
//                SpannableString("历次发布试卷总有效度:${info!![0].RecordEffective}%").apply {
//                    setSpan(ForegroundColorSpan(Color.parseColor("#22d7bb")),
//                            lastIndexOf(":") + 1, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//                }
//            }
//            tv_total.text = spanString
            tv_class.text = "${info!![0].CourseClassName}⇌"
            tv_class.setOnClickListener {
                classPopupWindow.setOnDismissListener {
                    view_alpha.visibility = View.GONE
                }
                classPopupWindow.showAsDropDown(tv_class, 0, 10)
                view_alpha.visibility = View.VISIBLE
            }
            tv_close.setOnClickListener { dismiss() }
        }
        super.onViewCreated(view, savedInstanceState)
    }

    @SuppressLint("SetTextI18n")
    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        classPopupWindow.dismiss()
        tv_num.text = "${info!![position]?.RecordEffective}%"
        tv_num_add.text = "(+${info!![0].ContributionRate}%)"
        tv_percent.text = "${info!![position].KlgRate}%"
        tv_percent_add.text = "(+${info!![0].KlgContributionRate}%)"
//        val spanString by lazy {
//            SpannableString("历次发布试卷总有效度:${info!![position].RecordEffective}%").apply {
//                setSpan(ForegroundColorSpan(Color.parseColor("#22d7bb")),
//                        lastIndexOf(":") + 1, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//            }
//        }
//        tv_total.text = spanString
        tv_class.text = "${info!![position].CourseClassName}⇌"
    }

    override fun onResume() {
        super.onResume()
        val param = this.dialog?.window?.attributes
        param?.width = DensityUtils.dp2px(context, 280f)
        param?.height = DensityUtils.dp2px(context, 240f)
        dialog?.window?.attributes = param
    }
}